/***
 * 微信 JS SDK
 */
var wxsdk = {
	/***
	 * 初始化
	 */
	initConfig : function(callback) {
		wx.error(function(res) {
			alert("wx err:" + res.errMsg);
		});
		wx.ready(function() {
			if(callback) {
				callback();
			}
		});
		//get sdk config
		$.ajax({
			url: '/wx/jsconfig?appid=' + gconsts.wx.appid,
			type: 'GET',
			dataType: 'json',
			data: { 'u':encodeURIComponent(window.location.href) },
			crossDomain: true,
			error: function(error) {
				console.log(err);
			},
			success: function(data) {
				var opt = {
					debug: false,
					appId: data.appId,
					timestamp:data.timestamp,
					nonceStr: data.nonceStr,
					signature: data.signature,
					jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage', 'closeWindow']
				};
				wx.config(opt);
			}
		});
	},

	/**
	 * 分享到朋友圈
	 */
	onShareTimeline : function(title,imageurl,link, callback) {
		if(wx && wx.onMenuShareTimeline){
			wx.onMenuShareTimeline({
				'title': title,
				'link': link,
				'imgUrl': imageurl,
				success: function () {
					if(callback){
						callback("success");
					}
				},
				cancel: function () {
					if(callback){
						callback("cancel");
					}
				}
			});
		}else{
			alert("weixin sdk not ready");
		}
	},

	/**
	 * 分享到好友
	 */
	onShareAppMessage : function(title,desc,imageurl,link, callback) {
		if(wx && wx.onMenuShareAppMessage){
			wx.onMenuShareAppMessage({
				'title': title,
				'desc': desc,
				'link': link,
				'imgUrl': imageurl,
				type: '',
				dataUrl: '',
				success: function () {
					if(callback){
						callback("success");
					}
				},
				cancel: function () {
					if(callback){
						callback("cancel");
					}
				}
			});
		}else{
			alert("weixin sdk not ready");
		}
	}
};